﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using GTServices.Entities;
using GTServices.ServiceContracts;

namespace GTServices.Service
{
    // Start the service and browse to http://<machine_name>:<port>/SampleService/help to view the service's generated help page
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class SampleService : IPatternService
    {

        public List<Pattern> GetPatterns()
        {
            return Database.Instance.Patterns.ToList(); 
        }

        public Pattern Create(Pattern instance)
        {
           
            // Add the new instance of SampleItem to the collection
            Database.Instance.Patterns.Add(instance);
            Database.Instance.SaveChanges();
            return Database.Instance.Patterns.FirstOrDefault((x) => x.Id == instance.Id);
          
        }

        public Pattern Get(int id)
        {
           //Return the instance of SampleItem with the given id
            return Database.Instance.Patterns.FirstOrDefault((x) => x.Id == id); 
         
        }

        public Pattern Update(int id, Pattern instance)
        {
            // Update the given instance of SampleItem in the collection
            Pattern toFind = Database.Instance.Patterns.FirstOrDefault((x) => x.Id == id);
            if (toFind != null)
            {
                Database.Instance.Patterns.Remove(toFind);
                Database.Instance.Patterns.Add(instance);
                Database.Instance.SaveChanges(); 
            }
            return toFind;
           
        }


        public void Delete(int id)
        {
            // Remove the instance of SampleItem with the given id from the collection
            Pattern bp = Database.Instance.Patterns.FirstOrDefault((x) => x.Id == id);
            if (bp != null)
            {
              Database.Instance.Patterns.Remove(bp);
            }
        }

    }
}
